package com.blank.task18;

import java.io.*;
import java.util.List;

public class ReadFileList {



    public static List<Student> readList() {
        File f = new File("d:/student/stuList.txt");
        //声明一个Student类型的List集合用来存储studentList
        List<Student> studentList = null;
        if(f.exists()){
            //声明ObjectInputStream类用于从输入流中一次性将对象整体读取出来
            ObjectInputStream ois = null;
            try {
                ois = new ObjectInputStream(new FileInputStream(f));
               studentList = (List<Student>) ois.readObject();
               //将原有的集合打印出来
                String str = studentList.toString();
                System.out.println(str);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                //关闭相应的流并释放资源
                if(null != ois){
                    try {
                        ois.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return studentList;
    }
}
